<template>
  <van-popup v-model="popupDateTime" round position="bottom">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="popupDateTime = false"
      @confirm="onConfirm"
    />
  </van-popup>
</template>
<script>
export default {
  props: {
    defaultDateTime: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      popupDateTime: false,
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: '',
      isNoData: false
    };
  },
  methods: {
    open() {
      const _defaultDateTime = this.defaultDateTime && this.defaultDateTime.replace(/-/g, '/') || '2000/01/01';
      this.currentDate = new Date(_defaultDateTime || '');
      this.popupDateTime = true;
    },
    onConfirm() {
      this.$emit('onConfirm', this.handleDateTimeOutput(this.currentDate));
      this.popupDateTime = false;
    },
    // 处理日期输出
    handleDateTimeOutput(date) {
      const _date = new Date(date);
      const _y = _date.getFullYear();
      const _m = _date.getMonth() + 1 > 9 ? _date.getMonth() + 1 : '0' + (_date.getMonth() + 1);
      const _d = _date.getDate() > 9 ? _date.getDate() : '0' + _date.getDate();
      return _y + '-' + _m + '-' + _d;
    }
  }
};
</script>
